package com.piesat.quake.business.station.api;

import com.piesat.common.annotation.rest.AnonymousPostMapping;
import com.piesat.common.vo.ResultVO;
import com.piesat.quake.business.station.model.StationInfo;
import com.piesat.quake.business.station.service.StationInfoService;
import com.piesat.quake.business.station.service.dto.StationQueryDTO;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/station")
@Api(value = "仪器：台站管理", tags = "仪器：台站管理")
@RequiredArgsConstructor
public class StationInfoController {

    private final StationInfoService stationInfoService;


    /**
     * 新增站点
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增台站", notes = "新增台站")
    public ResultVO saveStation(@RequestBody StationInfo info) {
        stationInfoService.saveStation(info);
        return ResultVO.success();
    }

    /**
     * 删除站点
     */
    @DeleteMapping(value = "/remove")
    @ApiOperation(value = "删除台站", notes = "删除台站")
    public ResultVO removeStation(String id) {
        stationInfoService.removeStation(id);
        return ResultVO.success();
    }

    /**
     * 更新站点
     */
    @PutMapping(value = "/update")
    @ApiOperation(value = "更新台站", notes = "更新台站")
    public ResultVO updateStation(@RequestBody StationInfo info) {
        stationInfoService.updateStation(info);
        return ResultVO.success();
    }

    /**
     * 根据区划CODE查询站点列表
     */
    @GetMapping(value = "/region/list")
    @ApiOperation(value = "根据区划code得到台站列表", notes = "根据区划code得到台站列表")
    public ResultVO findStationInfoList(String regionCode) {
        stationInfoService.findStationInfoList(regionCode);
        return ResultVO.success();
    }

    /**
     * 解绑站台
     */
    @PostMapping(value = "/unbind")
    @ApiOperation(value = "解除台站和区划的绑定", notes = "解除台站和区划的绑定")
    public ResultVO unbindStation(String stationId, String regionCode) {
        stationInfoService.unbindStation(stationId, regionCode);
        return ResultVO.success();
    }

    /**
     * 绑定站台
     */
    @PostMapping(value = "/bind")
    @ApiOperation(value = "绑定台站和区划", notes = "绑定台站和区划")
    public ResultVO bindStation(String stationId, String regionCode) {
        stationInfoService.bindStation(stationId, regionCode);
        return ResultVO.success();
    }

    @PostMapping(value = "/page")
    @ApiOperation(value = "分页查询站点信息", notes = "分页查询站点信息")
    public ResultVO selectByPage(@RequestBody StationQueryDTO dto) {
        return ResultVO.success(stationInfoService.selectByPage(dto));
    }

    @GetMapping(value = "/statistic")
    @ApiOperation(value = "/统计信息", notes = "统计信息")
    public ResultVO statistic() {
        return ResultVO.success(stationInfoService.statistic());
    }

//    @AnonymousPostMapping("/uploadExcel")
//    @PostMapping(value = "/uploadExcel")
//    @ApiOperation(value = "/上传站点excel文档")
//    public ResultVO uploadExcel(@RequestParam("file") MultipartFile file) {
//        boolean b = stationInfoService.uploadExcel(file);
//        if (b) {
//            return ResultVO.success("批量导入成功");
//        }
//        return ResultVO.failure("批量导入失败");
//    }

    @AnonymousPostMapping("/uploadExcel")
    @PostMapping(value = "/uploadExcel")
    @ApiOperation(value = "/上传站点excel文档")
    public void uploadExcel(HttpServletRequest request) {
        try {
            long l = System.currentTimeMillis();
            ServletInputStream inputStream = request.getInputStream();
            int length = 0;
            byte[] bytes = new byte[8 * 1024];
            FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\临时文件\\test.iso"));
            while ((length = inputStream.read(bytes)) > 0) {
                fileOutputStream.write(bytes, 0, length);
                System.out.println("写入8MB数据");
            }
            inputStream.close();
            fileOutputStream.close();
            System.out.println(System.currentTimeMillis()-l);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}

